<?php

namespace app\common\model;

use org\util\HelpUtil;
use think\facade\Session;
use think\facade\Validate;
use think\model\concern\SoftDelete;

class SiteModel extends BaseModel {
    protected $name = 'site';
    protected $pk = 'id';
    //开启自动写入时间戳
    protected $autoWriteTimestamp = true;
    //视图查询
    public function searchViewAttr($query, $value, $data) {

    }


    /**
     * 添加站点
     * @param $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function addData($data) {
        self::startTrans();
        try {
            if($data['trade_id']==0) exception('只有行业管理员才可以创建站点！',40001);
            $begin_end = explode(' - ',$data['begin_end']);
            $data['begin_time']=strtotime($begin_end[0]);
            $data['end_time']=strtotime($begin_end[1]);
            if(!isset($data['admin_id']) && ($data['username']=='' || $data['password']=='')){
                exception('请输入站点管理账号及密码',40001);
            }
            $info = self::create($data,true);
            /*创建店铺管理员*/
            if(!isset($data['admin_id'])){
                $adminModel = new AdminModel();
                $adminData =[
                    'username'=>$data['username'],
                    'password'=>$data['password'],
                    'nickname'=>$data['nickname'],
                    'session_key'=>'site',
                    'is_init'=>1
                ];
                $adminInfo = $adminModel->addData($adminData);
                $data['admin_id']=$adminInfo['id'];
            }
            /*创建店铺顶级部门*/
            $deptModel = new DeptModel();
            $deptData =[
                'pid'=>0,
                'name'=>$data['site_name'],
                'site_id'=>$info['id'],
                'trade_id'=>$data['trade_id']
            ];
            $deptInfo = $deptModel->addData($deptData);
            /*创建商品类目顶级*/
            /*创建文章类目顶级*/
            /*反写admin_id,dept_id,trade_id到权限表*/
            AdminAccessModel::create([
                'admin_id'=>$data['admin_id'],
                'site_id'=>$info['id'],
                'dept_id'=>$deptInfo['id'],
                'group_id'=>4,//暂时给与店铺权限
                'trade_id'=>$data['trade_id']
            ]);
            /*反写admin_id到店铺表*/
            $this->save([
                'owner_id'=>$data['admin_id'],
            ],['id'=>$info['id']]);
            self::commit();
        } catch (\Exception $e) {
            self::rollback();
            exception($e->getMessage(),40001);
        }
        return true;
    }

    /**
     * 修改行业站点
     * @param $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function editData($data) {
        self::startTrans();
        try {
            self::commit();
        } catch (\Exception $e) {
            self::rollback();
            exception($e->getMessage());
        }
        return true;
    }
}